package com.lishem.leftgod.level1.class03;

/**
 * 两个单链表相交的一系列问题
 * 在本题中，单链表可能有环，也可能无环。给定两个单链表的头节点head1和head2，
 * 这两个链表可能相交，也可能不相交。请实现一个函数，如果两个链表相交，请返回相交的第一个节点；如果不相交，返回null即可。
 */
public class Code_14_FindFirstIntersectNode {


    public static Node getIntersectNode(Node head1, Node head2) {
        Node loop1 = getListLoop(head1);
        Node loop2 = getListLoop(head2);
        if (loop1 == null && loop2 == null) {
            // 两个都无环
            return getIntersectNodeNoLoop(head1, head2);
        } else if (loop1 != null && loop2 != null) {
            // 两个都有环
            return getIntersectNodeWithLoop(head1, head2, loop1, loop2);
        } else {
            return null;
        }
    }

    private static Node getIntersectNodeWithLoop(Node head1, Node head2, Node loop1, Node loop2) {
        if (loop1 == loop2) {
            // 入环节点相同
            Node cur1 = head1;
            Node cur2 = head2;
            int len1 = 0;
            int len2 = 0;
            while (cur1 != loop1) {
                cur1 = cur1.next;
                ++len1;
            }
            while (cur2 != loop2) {
                cur2 = cur2.next;
                ++len2;
            }
            int dif = Math.abs(len1 - len2);
            if (len1 > len2) {
                cur1 = head1;
                cur2 = head2;
            } else {
                cur1 = head2;
                cur2 = head1;
            }
            while (dif > 0) {
                cur1 = cur1.next;
                dif--;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        }
        Node cur = loop1.next;
        while (cur != loop2 && cur != loop1) {
            cur = cur.next;
        }
        return cur == loop2 ? loop1 : null;
    }

    private static Node getIntersectNodeNoLoop(Node head1, Node head2) {
        int len1 = 0;
        int len2 = 0;
        Node cur1 = head1, cur2 = head2;
        while (cur1 != null) {
            cur1 = cur1.next;
            len1++;
        }
        while (cur2 != null) {
            cur2 = cur2.next;
            len2++;
        }
        if (len1 > len2) {
            cur1 = head1;
            cur2 = head2;
        } else {
            cur1 = head2;
            cur2 = head1;
        }
        cur1 = len1 > len2 ? head1 : head2;
        int dif = Math.abs(len1 - len2);
        while (dif != 0) {
            cur1 = cur1.next;
            dif--;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1 == cur2 ? cur1 : null;
    }

    private static Node getListLoop(Node head) {
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == slow) {
            fast = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
        }
        return null;
    }

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }


}
